package com.sgcc.epri.dcloud.conf.thread;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class ThreadPool {

    /*
     * # 线程池的配置文件
     */
    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10); // 设置核心线程数为5
        executor.setMaxPoolSize(20); // 设置最大线程数也为5，这样就不会有超过5个线程在运行
        executor.setQueueCapacity(Integer.MAX_VALUE); // 设置队列容量，当所有5个线程都在忙时，新任务将进入队列等待
        executor.setThreadNamePrefix("Async-"); // 设置线程名的前缀
        executor.initialize(); // 初始化线程池
        return executor;
    }

}
